package com.irdstudio.bsp.executor.core.dao;

import com.irdstudio.bsp.executor.core.dao.domain.SRole;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色控制配置表类-[表名: 角色控制配置表(s_role)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-13
 */
public class SRoleDao {

	/* 连接对象 */
	Connection conn = null;	

	public SRoleDao(Connection conn){
		this.conn = conn;
	}


    /**
	* 新增一行
	* @return int
	*/
    public int insertSRole(SRole sRole) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("insert into s_role ( "
                + "roleno,rolename,orderno,type,memo,orgid"
                + ") values ("+ "?,?,?,?,?,?" +")"
                );
            ps.setObject(1, sRole.getRoleno());
            ps.setObject(2, sRole.getRolename());
            ps.setObject(3, sRole.getOrderno());
            ps.setObject(4, sRole.getType());
            ps.setObject(5, sRole.getMemo());
            ps.setObject(6, sRole.getOrgid());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("insert SRole is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键删除一行记录
	* @return int
	*/
    public int deleteByPk(SRole sRole) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("delete from s_role where 1=1"
                + " AND roleno = ? "
                );
            ps.setObject(1, sRole.getRoleno());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("delete SRole is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键更新一行记录
	* @return int
	*/
    public int updateByPk(SRole sRole) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("update s_role set "
                + " roleno = ? ,"
                + " rolename = ? ,"
                + " orderno = ? ,"
                + " type = ? ,"
                + " memo = ? ,"
                + " orgid = ? "
                + " where 1=1"
                + " AND roleno = ? "
                );
            ps.setObject(1, sRole.getRoleno());
            ps.setObject(2, sRole.getRolename());
            ps.setObject(3, sRole.getOrderno());
            ps.setObject(4, sRole.getType());
            ps.setObject(5, sRole.getMemo());
            ps.setObject(6, sRole.getOrgid());
            ps.setObject(7, sRole.getRoleno());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("update SRole is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }

	}

    /**
	* 根据主键查询一行记录
	* @return int
	*/
    public SRole queryByPk(SRole sRole) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        SRole result = null;
        try {
            ps = conn.prepareStatement("select roleno,rolename,orderno,type,memo,orgidfrom s_role where 1=1 "
                + " AND roleno = ? "
                );
            ps.setObject(1, sRole.getRoleno());
            rs = ps.executeQuery();
            if (rs.next()) {
				result = new SRole();
				result.setRoleno(rs.getString("roleno"));
				result.setRolename(rs.getString("rolename"));
				result.setOrderno(rs.getBigDecimal("orderno"));
				result.setType(rs.getBigDecimal("type"));
				result.setMemo(rs.getString("memo"));
				result.setOrgid(rs.getString("orgid"));
			}
        } catch (SQLException e) {
            throw new SQLException("update SRole is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	* 根据 sRole 参数列表查询 不分页
	* @return int
	*/
    public List<SRole> queryAll(SRole sRole) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<SRole> result = new ArrayList<>();
        try {
            String sql = "select roleno,rolename,orderno,type,memo,orgidfrom s_role where 1=1 ";
                if (sRole.getRoleno() != null) {
                    sql = sql + " AND roleno =  '" + sRole.getRoleno() + "'";
                }
                if (sRole.getRolename() != null) {
                    sql = sql + " AND rolename =  '" + sRole.getRolename() + "'";
                }
                if (sRole.getOrderno() != null) {
                    sql = sql + " AND orderno =  '" + sRole.getOrderno() + "'";
                }
                if (sRole.getType() != null) {
                    sql = sql + " AND type =  '" + sRole.getType() + "'";
                }
                if (sRole.getMemo() != null) {
                    sql = sql + " AND memo =  '" + sRole.getMemo() + "'";
                }
                if (sRole.getOrgid() != null) {
                    sql = sql + " AND orgid =  '" + sRole.getOrgid() + "'";
                }
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
				SRole obj = new SRole();
				obj.setRoleno(rs.getString("roleno"));
				obj.setRolename(rs.getString("rolename"));
				obj.setOrderno(rs.getBigDecimal("orderno"));
				obj.setType(rs.getBigDecimal("type"));
				obj.setMemo(rs.getString("memo"));
				obj.setOrgid(rs.getString("orgid"));
                result.add(obj);
			}
        } catch (SQLException e) {
            throw new SQLException("update SRole is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
    protected void close(ResultSet theRs, Statement theStmt, PreparedStatement thePs) {
        try {
            if (theRs != null)
                theRs.close();
            if (theStmt != null)
                theStmt.close();
            if (thePs != null)
                thePs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
